home *** CD-ROM | disk | FTP | other *** search
- Path: news.salford.ac.uk!aber!not-for-mail
- From: auj@aber.ac.uk (Alun "Da Penguin" Jones)
- Newsgroups: comp.lang.c
- Subject: Re: Determining the length of an int in string form
- Date: 28 Mar 1996 14:52:49 -0000
- Organization: Prifysgol Cymru (University of Wales) Aberystwyth
- Message-ID: <4je941$hup@osfa.aber.ac.uk>
- References: <3146D058.DD7@cbm.com> <4i7uth$qph@inet-nntp-gw-1.us.oracle.com> <DoE38u.GIH@iquest.net> <827163143snz@genesis.demon.co.uk>
- NNTP-Posting-Host: osfa.aber.ac.uk
-
- Further up the screen fred@genesis.demon.co.uk wrote:
- >
- >>wkaufman@wkaufman.us.oracle.com (William Kaufman) wrote:
- >>+In article <3146D058.DD7@cbm.com> Dave Payne <paynedc@cbm.com> writes:
- >>+] Consider this:
- >>+]
- >>+] I have a variable of type int, and I would like to use the sprintf()
- >>+] function to write this variable to a string. However, I want to
- >>+] dynamically allocate the space for the string, and only malloc enough
- >>+] space to hold the int.
- >>
- >>WHY ???
- >>
- >>Even a 32-bit int is only 9 characters at most, and that's *with* the sign.
- >
- >-2147483647 is a valid 32 bit int and is 11 characters (excluding the null
- >character string terminator). The problem of course is that ints aren't
- >limited to 32 bits - the language imposes no upper limit to their size.
-
- True... If you're working in 2's complement, then you should be able to
- calculate the maximum, including the sign and null as follows:
-
- (int) (n*log(2)/log(10)) + 3
-
- where n=number of bits.
-
- Cheers,
- Alun.
- --
- $_='\=*Sxw!jds@j$.jl.dt#Rw%^dcn"K1x(=Bl1nwl!\*1enab^h"F=!J$h%fhcq',
- tr&J-ZA-Ij-za-i&A-Za-z&&s&\(&logic&&&s&\*&un&g&s&=&al&g&s&\^&it&g&&
- s&%&st&g&&s&\$&ber&g&s&\#&\n&&s&"& of&g,s&([A-Z])& $1&g&&s&\\u&U&&&
- s&!&es, &g&s&\\a&A&&s&1&i&g&&print" $_\n";sub liminal{"use perl!";}
-